<?php

class UserController extends Zend_Controller_Action
{

    private  $auth;
    private $message;
    public function init()
    {
      $this->auth=  Zend_Auth::getInstance();
         if(!$this->auth->hasIdentity() && $_SESSION['role'] != "A"){
             $this->_redirect('index');
      }
    }

    public function indexAction()
    {     
      if($_SESSION['role']=='R'){
           $this->view->title="Manage Profile";
            $this->_redirect("user/profile");
       }elseif($_SESSION['role']=='A'){
             $this->view->title="Add User";
             $this->_redirect("user/add");
       }
    }

    public function addAction(){
        try{
             $this->view->title="Add User - Welcome to Rosemoore Lake";
            if(isset($_POST['action'])){
                    $users = new Application_Model_Users();
                    $users->setEmailAddress($_POST['emailAddress']);
                    $users->setPassword($users->createRandomPassword());
                    $users->setFirstName($_POST['firstName']);
                    $users->setLastName($_POST['lastName']);
                    $users->setAddress1($_POST['address1']);
                    $users->setAddress2($_POST['address2']);
                    $users->setCity($_POST['city']);
                    $users->setState($_POST['state']);
                    $users->setCountry($_POST['country']);
                    $users->setZip($_POST['zip']);
                    $users->setPhone1($_POST['phone1']);
                    $users->setPhone2($_POST['phone2']);
                    $users->setCell($_POST['cell']);
                    $users->setEmail2($_POST['email2']);
                    if(isset ($_POST['option1'])){
                         $users->setResident($_POST['option1']);
                    }else{
                        $users->setResident("N");
                    }
                    if(isset ($_POST['option2'])){
                        $users->setAdmin($_POST['option2']);
                    }else{
                        $users->setAdmin("N");
                    }
                    $users->setCreatedBy($this->auth->getStorage()->read()->EMAIL_ADDRESS);
                    $date = new Zend_Date();
                    $users->setCreatedDate($date->getIso());
                    $users->setActiveFlag("Y");
                    $users->setChangePassword("Y");
                    $userMapper = new Application_Model_UserMapper();

                    $userMapper->insert($users);

                    $this->view->message="<img src='../images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> User added Successfully</font>";
               }
        }catch(Exception $e){
            Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> ".$e->getMessage()."</font>";
        }
    }

     public function editAction(){
         try{
                     $this->view->title="Edit User - Welcome to Rosemoore Lake";
                     if(isset($_POST['action']) && $_POST['action'] == "_search"){      
                          $users = new Application_Model_Users();
                          $users->setFirstName($_POST['_firstName']);
                          $users->setLastName($_POST['_lastName']);
                          $users->setAddress1($_POST['_address1']);

                          $this->view->firstName =$_POST['_firstName'];
                          $this->view->lastName=$_POST['_lastName'];
                          $this->view->address1 =$_POST['_address1'];

                          $userMapper = new Application_Model_UserMapper();
                          $rows = $userMapper->find($users);  
                          $this->view->result= $rows;
                      }elseif(isset($_POST['action']) && $_POST['action'] == "_sort"){
                              $users = new Application_Model_Users();
                              $users->setFirstName($_POST['_firstName']);
                              $users->setLastName($_POST['_lastName']);
                              $users->setAddress1($_POST['_address1']);
                              $users->setSortBy($_POST['_sortBy']);

                              $this->view->firstName =$_POST['_firstName'];
                              $this->view->lastName=$_POST['_lastName'];
                              $this->view->address1 =$_POST['_address1'];
                              $this->view->sortBy =$_POST['_sortBy'];

                              $userMapper = new Application_Model_UserMapper();
                              $rows = $userMapper->find($users);
                              $this->view->result= $rows;
                     }elseif(isset($_POST['action']) && $_POST['action'] == "_edit"){
                          
                             $this->view->firstName = $_POST['_firstName'];
                             $this->view->lastName=$_POST['_lastName'];
                             $this->view->address1=$_POST['_address1'];

                             $users = new Application_Model_Users();
                             $users->setUserId($_POST['userId']);
      
                             $userMapper = new Application_Model_UserMapper();
                             $rows = $userMapper->find($users);
             
                            foreach ($rows as $data) {
                                $users->setUserId($data->USER_ID)
                                        ->setEmailAddress($data->EMAIL_ADDRESS)
                                        ->setFirstName($data->FIRST_NAME)
                                        ->setLastName($data->LAST_NAME)
                                        ->setAddress1($data->ADDRESS_LINE_1)
                                        ->setAddress2($data->ADDRESS_LINE_2)
                                        ->setCity($data->CITY)
                                        ->setState($data->STATE)
                                        ->setCountry($data->COUNTRY)
                                        ->setZip($data->ZIP)
                                        ->setPhone1($data->PHONE_NUMBER_1)
                                        ->setPhone2($data->PHONE_NUMBER_2)
                                        ->setCell($data->CELL_PHONE)
                                        ->setEmail2($data->EMAIL_2)
                                        ->setAdmin($data->ADMIN)
                                        ->setResident($data->RESIDENT)
                                        ->setActiveFlag($data->ACTIVE_FLAG);
                           }
                           $this->view->data= $users ;

                      }elseif(isset($_POST['action']) && $_POST['action'] == "_print"){
                          try{
                              $users = new Application_Model_Users();
                              $users->setFirstName($_POST['_firstName']);
                              $users->setLastName($_POST['_lastName']);
                              $users->setAddress1($_POST['_address1']);
                              $users->setSortBy($_POST['_sortBy']);

                              $this->view->firstName =$_POST['_firstName'];
                              $this->view->lastName=$_POST['_lastName'];
                              $this->view->address1 =$_POST['_address1'];
                              $this->view->sortBy =$_POST['_sortBy'];

                              $userMapper = new Application_Model_UserMapper();
                              $rows = $userMapper->find($users);

                           $pdf = new Zend_Pdf();

                            $currentPage = 0;

                            $pdf->pages[$currentPage] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);


//                           var_dump($pdf);exit();
                            header("Content-Disposition: inline; filename=UserList.pdf");
                            header("Content-type: application/x-pdf");
                            echo $pdf->render();
                            exit();
                         }catch(Zend_Pdf_Exception $e){
                                   Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                                    die ('PDF error: ' . $e->getMessage());  
                           }catch(Exception $e){
                             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                             die ('Application error: ' . $e->getMessage());   
                           }
                      }
                      elseif(isset($_POST['action']) && $_POST['action'] == "_save"){
                            $this->view->firstName = $_POST['_firstName'];
                            $this->view->lastName=$_POST['_lastName'];
                            $this->view->address1=$_POST['_address1'];

                            $users = new Application_Model_Users();
                            $users->setUserId($_POST['userId']);
                            $users->setEmailAddress($_POST['emailAddress']);
                            $users->setFirstName($_POST['firstName']);
                            $users->setLastName($_POST['lastName']);
                            $users->setAddress1($_POST['address1']);
                            $users->setAddress2($_POST['address2']);
                            $users->setCity($_POST['city']);
                            $users->setState($_POST['state']);
                            $users->setCountry($_POST['country']);
                            $users->setZip($_POST['zip']);
                            $users->setPhone1($_POST['phone1']);
                            $users->setPhone2($_POST['phone2']);
                            $users->setCell($_POST['cell']);
                            $users->setEmail2($_POST['email2']);
                            if(isset ($_POST['option1'])){
                                 $users->setResident($_POST['option1']);
                            }else{
                                $users->setResident("N");
                            }
                            if(isset ($_POST['option2'])){
                                $users->setAdmin($_POST['option2']);
                            }else{
                                $users->setAdmin("N");
                            }
                            if(isset ($_POST['active'])){
                                $users->setActiveFlag($_POST['active']);
                            }else{
                                $users->setActiveFlag("N");
                            }
                            $users->setUpdatedBy($this->auth->getStorage()->read()->EMAIL_ADDRESS);
                            $date = new Zend_Date();
                            $users->setUpdatedDate($date->getIso());

                    
                            $userMapper = new Application_Model_UserMapper();

                            $userMapper->update($users);

                            $this->view->data= $users ;

                    
                             $this->view->message="<img src='../images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> User successfully updated</font>";
       
                      }
               }catch(Exception $e){
                   Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                    $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> ".$e->getMessage()."</font>";
        }
    }

        public function profileAction(){
            try{
                    $this->view->title="Manage Profile - Welcome to Rosemoore Lake";
                if(isset ($_POST['action'])){

                            $users = new Application_Model_Users();
                            $users->setUserId($_POST['userId']);
                            $users->setEmailAddress($_POST['emailAddress']);
                            $users->setFirstName($_POST['firstName']);
                            $users->setLastName($_POST['lastName']);
                            $users->setAddress1($_POST['address1']);
                            $users->setAddress2($_POST['address2']);
                            $users->setCity($_POST['city']);
                            $users->setPhone1($_POST['phone1']);
                            $users->setPhone2($_POST['phone2']);
                            $users->setCell($_POST['cell']);
                            $users->setEmail2($_POST['email2']);

                            $users->setCountry($_POST['_country']);
                            $users->setState($_POST['_state']);
                            $users->setZip($_POST['_zip']);

                            $users->setUpdatedBy($this->auth->getStorage()->read()->EMAIL_ADDRESS);
                            $date = new Zend_Date();
                            $users->setUpdatedDate($date->getIso());


                            $userMapper = new Application_Model_UserMapper();

                            $userMapper->updateProfile($users);

                           $this->view->message="<img src='../images/check.png'/>&nbsp;&nbsp;&nbsp<font color='green'> Profile successfully updated</font>";

                }else{
                             $users = new Application_Model_Users();
                             $users->setUserId($this->auth->getStorage()->read()->USER_ID);

                             $userMapper = new Application_Model_UserMapper();
                             $rows = $userMapper->find($users);

                            foreach ($rows as $data) {
                                $users->setUserId($data->USER_ID)
                                        ->setEmailAddress($data->EMAIL_ADDRESS)
                                        ->setFirstName($data->FIRST_NAME)
                                        ->setLastName($data->LAST_NAME)
                                        ->setAddress1($data->ADDRESS_LINE_1)
                                        ->setAddress2($data->ADDRESS_LINE_2)
                                        ->setCity($data->CITY)
                                        ->setState($data->STATE)
                                        ->setCountry($data->COUNTRY)
                                        ->setZip($data->ZIP)
                                        ->setPhone1($data->PHONE_NUMBER_1)
                                        ->setPhone2($data->PHONE_NUMBER_2)
                                        ->setCell($data->CELL_PHONE)
                                        ->setEmail2($data->EMAIL_2)
                                        ->setAdmin($data->ADMIN)
                                        ->setResident($data->RESIDENT)
                                        ->setActiveFlag($data->ACTIVE_FLAG);
                           }
                }
                           $this->view->data= $users ;
            }catch(Exception $e){
                Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                $this->view->message="<img src='../images/cross.png'/>&nbsp;&nbsp;&nbsp<font color='red'> ".$e->getMessage()."</font>";
            }
       }
}

